﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Reflection;

namespace Public.MvvmBase
{
    /// <summary>
    /// 视图模型 基类
    /// </summary>
    public class PropertyChangedBase : INotifyPropertyChanged
    {
        /// <summary>
        /// INotifyPropertyChanged 接口的实现
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChangedBase(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChangedEventArgs pcec = new PropertyChangedEventArgs(name);
                PropertyChanged(this, pcec);
            }
        }

        /// <summary>
        /// 属性改变 出发事件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="exp"></param>
        public void OnPropertyChanged<T>(Expression<Func<T>> exp)
        {
            var body = exp.Body as System.Linq.Expressions.MemberExpression;
            if (body == null)
            {
                throw new ArgumentException("member is null");
            }

            var info = body.Member as PropertyInfo;
            if (info == null)
            {
                throw new ArgumentException("property is null");
            }

            var name = info.Name;
            this.OnPropertyChangedBase(name);
        }
    }
}
